import path from 'path';
import express from 'express';
import logger from '../common/logger';

import { initServices } from '../domain/service';

// Controllers (route handlers)
import * as agentController from './controller/agentController';
import * as definitionController from './controller/definitionController';
import * as simulationJobController from './controller/simulationJobController';
import * as parameterController from './controller/parameterController';
import * as simModelController from './controller/simModelController';
import * as productController from './controller/productController';

const app = express();

// Express configuration
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, '../views'));
app.set('view engine', 'pug');

app.use(
  express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 })
);

app.use(express.json({ limit: '500mb' }));
app.use(express.urlencoded({ limit: '500mb', extended: false }));

agentController.init(app);
definitionController.init(app);
simulationJobController.init(app);
parameterController.init(app);
simModelController.init(app);
productController.init(app);

app.use((err, req, res, next) => {
  logger.error(`runtime error ${err.message}`);
  res.json({
    success: false,
    message: `${err.message}`
  });
});

initServices();

export default app;